﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Zebra.Umbraco.Marcos.Code.QuestionType
{
    [Serializable]
    public class RegularQuestion : Question
    {
        public SortType SortType
        {
            get;
            set;
        }

        public bool AllowComment
        {
            get;
            set;
        }

        public bool MultipleSelection
        {
            get;
            set;
        }

        public string AnswerText
        {
            get;
            set;
        }

        protected override string GenerateBodyHTML()
        {
            return BuildAnswerHTML();
        }

        public override QuestionCatalog Catalog
        {
            get
            {
                return QuestionCatalog.RegularQuestion;
            }
        }
        private string BuildAnswerHTML()
        {
            string retVal = "";
            List<string> answers = new List<string>();
            foreach (string str in AnswerText.Split('\n'))
            {
                if (!string.IsNullOrEmpty(str))
                {
                    answers.Add(str);
                }
            }
            if (SortType == QuestionType.SortType.sequence)
            {
                for (int index = 0; index < answers.Count; index++)
                {
                    retVal += Build(answers[index], index);
                }
            }
            else if (SortType == QuestionType.SortType.Flip)
            {
                if (new Random().Next(0, 2) == 1)
                {
                    for (int index = answers.Count - 1; index >= 1; index--)
                    {
                        retVal += Build(answers[index], index);
                    }
                }
                else
                {
                    for (int index = 0; index < answers.Count; index++)
                    {
                        retVal += Build(answers[index], index);
                    }
                }
            }
            else
            {
                while (answers.Count > 1)
                {
                    int index = new Random().Next(0, answers.Count);
                    retVal += Build(answers[index], index);
                    answers.RemoveAt(index);
                }
            }




            if (AllowComment)
            {
                retVal += BuildCommentHTML();
            }
            return retVal;
        }

        private string Build(string answer, int index)
        {
            if (MultipleSelection)
            {
                return BuildCheckBox(answer, index);
            }
            else
            {
                return BuildRadioButton(answer, index);
            }
        }

        private string BuildCommentHTML()
        {
            return string.Format("<input type=\"text\" size=\"50\" name=\"{0}\" id=\"{0}\"/>", string.Format("Comment_", QuestionID));
        }


    }
}